package HtServer.grpc;

import com.google.protobuf.ByteString;
import grpc.htsoft.meeting.*;
import io.grpc.stub.StreamObserver;

public class MeetingGreeter  extends MeetingGreeterGrpc.MeetingGreeterImplBase {
    @Override
    public void getUser(userRequest request, StreamObserver<user> responseObserver) {

        System.out.println("getUser");
        String id = request.getId();
        String name = request.getName();
        System.out.println("id: " + id + " name: " + name);
        user reply = user.newBuilder().setName("getUser " + request.getName())
//                (ByteString.copyFromUtf8(""))
                .setImagebase64("")
                .setContent("content 你好")
                .setDept("开发部")
                .setGroupname("第一组")
                .setGroupid("1")


                .build();
        responseObserver.onNext(reply);
        responseObserver.onCompleted();
    }

    @Override
    public void getGroup(grouprequest request, StreamObserver<usergroup> responseObserver) {
        System.out.println("getGroup");
        String groupid = request.getGroupid();
        user u1 = user.newBuilder().setName("getUser zgw").setDept("开发部门")
                .setGroupname("第一小组")
                .setGroupid("1")
                .setContent("你好呀")
                .setLike(10)

                .build();
        user u2 = user.newBuilder().setName("getUser zgw2").setDept("开发部门2")
                .setGroupname("第一小组").build();

        usergroup ug = usergroup.newBuilder().setId("1").addUsers(u1).addUsers(u2)
                .setName("第一小组").build();
        responseObserver.onNext(ug);
        responseObserver.onCompleted();

//        super.getGroup(request, responseObserver);
    }

    @Override
    public void saveUser(user request, StreamObserver<commonReply> responseObserver) {
        System.out.println("getGroup");
        user u = request;
        System.out.println(u);
        System.out.println("save user");

        String id = u.getId();
        if(id==null || id.trim().equals(""))
        {
            System.out.println("new insert into ");
        }
        else {
            System.out.println("update from");
        }
        System.out.println(u);
        System.out.println("content = "+u.getContent());
        System.out.println("getId = "+u.getId());
        System.out.println("getName = "+u.getName());
        System.out.println("getGroupid = "+u.getGroupid());

        commonReply cr = commonReply.newBuilder().setMessage("success").build();

        responseObserver.onNext(cr);
        responseObserver.onCompleted();
    }

    @Override
    public void postUserContent(userConentRequest request, StreamObserver<commonReply> responseObserver) {
        super.postUserContent(request, responseObserver);
    }

    @Override
    public void getAllUser(Empty request, StreamObserver<AllUser> responseObserver) {
        super.getAllUser(request, responseObserver);
    }

    @Override
    public void getAllGroup(Empty request, StreamObserver<AllGroup> responseObserver) {
        super.getAllGroup(request, responseObserver);
    }
}
